//
//  TKL_CityDetailVC.m
//  TKL_EnPro
//
//  Created by 雅恩三河科技 on 2019/4/28.
//  Copyright © 2019年 MrZhang. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "TKL_CityDetailVC.h"
#import "Head.h"
#import "TKL_ForPhVC.h"
@interface TKL_CityDetailVC ()<SDCycleScrollViewDelegate>
@property(nonatomic,strong)NSMutableArray* typeArr;
@property(nonatomic,strong)BmobObject* dataSource;
@property(nonatomic,strong)NSMutableArray* bannerData;
@property(nonatomic,strong)UIView* btomView;
@property(nonatomic,strong)BmobObject* colObj;
@property(nonatomic,strong)UIButton* btomBtn;
@end

@implementation TKL_CityDetailVC
#pragma mark - getter and setter
-(UIButton *)btomBtn
{
    if (!_btomBtn)
    {
        @WeakSelf;
        _btomBtn = [[UIButton alloc]init];
        _btomBtn.width=WFCGFloatX(375);
        _btomBtn.height=WFCGFloatY(45);
        _btomBtn.x=WFCGFloatX(0);
        _btomBtn.y=SCREEN_HEIGHT-[self bottomShelterHeight]-WFCGFloatY(45);;
        [_btomBtn setEnlargeEdge:15];
        [_btomBtn setTitle:self.type==1?@"立即预约":@"我要拍同款" forState:UIControlStateNormal];
        [_btomBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btomBtn setBackgroundColor:Main_Color forState:UIControlStateNormal];
        _btomBtn.titleLabel.font = SYSTEMFONT(15);
        
        [_btomBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            
            TKL_ForPhVC* vc=[[TKL_ForPhVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _btomBtn;
}
-(UIView *)btomView
{
    @WeakSelf;
    if (!_btomView)
    {
        _btomView=[UIView new];
        _btomView.x=WFCGFloatX(0);
        _btomView.y=SCREEN_HEIGHT-WFCGFloatY(49)-[self bottomShelterHeight];
        _btomView.height=WFCGFloatY(49);
        _btomView.width=WFCGFloatX(375);
        _btomView.backgroundColor=[UIColor whiteColor];
        
        UIButton* colBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        UIImage* image=[UIImage imageNamed:@"detail_col_sel"];
        [colBtn setBackgroundImage:image forState:UIControlStateSelected];
        [colBtn setBackgroundImage:[UIImage imageNamed:@"detail_col"] forState:UIControlStateNormal];
        colBtn.width=23;
        colBtn.height=23;
        colBtn.x=WFCGFloatX(20);
        colBtn.tag=9000;
        colBtn.y=(_btomView.height-colBtn.height)/2;
        [colBtn setEnlargeEdge:15];
        [colBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            
            
            if (colBtn.selected==YES)
            {
                
                BmobObject *bmobObject = [BmobObject objectWithoutDataWithClassName:@"col"  objectId:self.colObj.objectId];
                [bmobObject deleteInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
                    if (isSuccessful) {
                        //删除成功后的动作
                        NSLog(@"successful");
                        colBtn.selected=NO;
                        self.colObj=nil;
                        [FTIndicator showSuccessWithMessage:@"取消收藏"];
                    } else if (error){
                        NSLog(@"%@",error);
                    } else {
                        NSLog(@"UnKnow error");
                    }
                }];
                
            }else
            {
                BmobObject  *gameScore = [BmobObject objectWithClassName:@"col"];
                [gameScore setObject:[selfp.dataSource objectForKey:@"name"] forKey:@"name"];
                [gameScore setObject:[selfp.dataSource objectForKey:@"image"] forKey:@"image"];
                [gameScore setObject:[selfp.dataSource objectForKey:@"price"] forKey:@"price"];
                [gameScore setObject:[selfp.dataSource objectForKey:@"old_price"] forKey:@"old_price"];
                [gameScore setObject:[selfp.dataSource objectForKey:@"sale"] forKey:@"sale"];
                [gameScore setObject:[selfp.dataSource objectForKey:@"mark"] forKey:@"mark"];
                
                //异步保存到服务器
                [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                    if (isSuccessful) {
                        NSLog(@"%@",gameScore);
                        colBtn.selected=YES;
                        [FTIndicator showSuccessWithMessage:@"收藏成功"];
                    } else if (error){
                        //发生错误后的动作
                        NSLog(@"%@",error);
                    } else {
                        NSLog(@"Unknow error");
                    }
                }];
            }
            
        }];
        [_btomView addSubview:colBtn];
        
        
        UIButton* phoneBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        UIImage* image11=[UIImage imageNamed:@"detail_phone"];
        [phoneBtn setBackgroundImage:image11 forState:UIControlStateNormal];
        phoneBtn.width=23;
        phoneBtn.height=23;
        phoneBtn.x=colBtn.right+WFCGFloatX(30);
        phoneBtn.y=(_btomView.height-phoneBtn.height)/2;
        [phoneBtn setEnlargeEdge:15];
        [phoneBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            // [[GCPresenter shareInterface] phoneCall:[self.dataSource objectForKey:@"mobile"]];
        }];
        [_btomView addSubview:phoneBtn];
        
        
        
        
        
        UIButton* cartBtn = [[UIButton alloc]init];
        cartBtn.width=WFCGFloatX(120);
        cartBtn.height=WFCGFloatY(49);
        cartBtn.x=WFCGFloatX(135);
        cartBtn.y=WFCGFloatY(0);
        [cartBtn setEnlargeEdge:15];
        [cartBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        [cartBtn setTitleColor:HEXCOLOR(@"#ffffff") forState:UIControlStateNormal];
        [cartBtn setBackgroundColor:HEXCOLOR(@"#EB6000") forState:UIControlStateNormal];
        cartBtn.titleLabel.font = SYSTEMFONT(15);
        
        [cartBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            
            
            //            if ([RDLoginViewModel shareInterface].isLogin) {
            //                BmobObject  *gameScore = [BmobObject objectWithClassName:@"cart"];
            //
            //                [gameScore setObject:[selfp.dataSource objectForKey:@"name"] forKey:@"name"];
            //                [gameScore setObject:[selfp.dataSource objectForKey:@"image"] forKey:@"image"];
            //                [gameScore setObject:[selfp.dataSource objectForKey:@"price"] forKey:@"price"];
            //                [gameScore setObject:[selfp.dataSource objectForKey:@"mark"] forKey:@"mark"];
            //                [gameScore setObject:[selfp.dataSource objectForKey:@"sale"] forKey:@"sale"];
            //                [gameScore setObject:[selfp.dataSource objectForKey:@"old_price"] forKey:@"old_price"];
            //                [gameScore setObject:@"1" forKey:@"count"];
            //                [gameScore setObject:selfp.goodID forKey:@"goodsid"];
            //                [gameScore setObject:[RDLoginViewModel shareInterface].userID forKey:@"userid"];
            //
            //                //异步保存到服务器
            //                [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            //                    if (isSuccessful) {
            //                        //创建成功后会返回objectId，updatedAt，createdAt等信息
            //                        //创建对象成功，打印对象值
            //                        NSLog(@"%@",gameScore);
            //                        [FTIndicator showSuccessWithMessage:@"成功加入购车"];
            //                    } else if (error){
            //                        //发生错误后的动作
            //                        NSLog(@"%@",error);
            //                    } else {
            //                        NSLog(@"Unknow error");
            //                    }
            //                }];
            //            }else
            //            {
            //                [FTIndicator showInfoWithMessage:@"去登录"];
            //            }
            
            
            
        }];
        
        [_btomView addSubview:cartBtn];
        
        UIButton* buyBtn = [[UIButton alloc]init];
        buyBtn.width=WFCGFloatX(120);
        buyBtn.height=WFCGFloatY(49);
        buyBtn.x=WFCGFloatX(255);
        buyBtn.y=WFCGFloatY(0);
        [buyBtn setEnlargeEdge:15];
        [buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
        [buyBtn setTitleColor:HEXCOLOR(@"#ffffff") forState:UIControlStateNormal];
        [buyBtn setBackgroundColor:HEXCOLOR(@"#CB0000") forState:UIControlStateNormal];
        buyBtn.titleLabel.font = SYSTEMFONT(15);
        
        [buyBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            //            RDMakeOrderVC* vc=[[RDMakeOrderVC alloc] init];
            //            vc.goodID=selfp.goodID;
            //            vc.passGoods=[[NSMutableArray alloc] initWithArray:@[self.dataSource]];;
            //            [selfp.navigationController pushViewController:vc animated:YES];
        }];
        
        [_btomView addSubview:buyBtn];
        
    }
    return _btomView;
}
-(NSMutableArray *)typeArr
{
    if (!_typeArr)
    {
        _typeArr=[NSMutableArray new];
    }
    return _typeArr;
}
-(BmobObject *)dataSource
{
    if (!_dataSource) {
        _dataSource=[BmobObject new];
    }
    return _dataSource;
}
-(NSMutableArray *)bannerData
{
    if (!_bannerData) {
        _bannerData=[NSMutableArray new];
    }
    return _bannerData;
}
#pragma mark - event
#pragma mark - method

#pragma mark - delegate
-(void)setcurrentTable
{
    @WeakSelf;
    self.baseTable.contentInset = UIEdgeInsetsMake([self topShelterHeight], 0, [self bottomShelterHeight]+WFCGFloatY(45), 0);
    self.view.backgroundColor = [UIColor whiteColor];
    [self.baseTable setSeparatorColor:HEXCOLOR(@"#F1F1F1")];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [selfp reloadData];
        [self.baseTable.mj_header endRefreshing];
    }];
    
    [self.view addSubview:self.baseTable];
    [self.view addSubview:self.btomBtn];
    
    
    [self viewModelBand];
    [self reloadData];
}
-(void)initilization
{
    self.isGroup = YES;
    
    [self setNavgationTheme:0];
    [self.navigationItem setTitle:[_passObj objectForKey:@"title"]];
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    [self setcurrentTable];
}
-(void)viewModelBand
{
    
}
-(void)reloadData
{
    [FTIndicator showProgressWithMessage:@"正在加载"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [FTIndicator dismissProgress];
    });
    
}
-(void)requestCol
{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSArray* arr=[self.passObj objectForKey:@"datas"];
    if (arr.count>4) {
        return 4;
    }
    return arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    @WeakSelf;
    
    static NSString* identify=@"ppPhonecel11l";
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor whiteColor];
    }
    [cell.contentView removeAllSubviews];
    NSArray* arr=[self.passObj objectForKey:@"datas"];
    UIImageView* markIV=[UIImageView new];
    markIV.width=WFCGFloatX(375);
    // markIV.height=WFCGFloatY(180);
    markIV.x=WFCGFloatX(0);
    markIV.y=-WFCGFloatY(0);
    // [markIV sd_setImageWithURL:[NSURL URLWithString:[arr[indexPath.row] objectForKey:@"fileUrl"]] placeholderImage:DefaultImage];
    [cell.contentView addSubview:markIV];
    
    
    [markIV sd_setImageWithURL:[NSURL URLWithString:arr[indexPath.row]] placeholderImage:DefaultImage options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        NSLog(@"-----%ld",receivedSize);
        
    } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        if (!error)
        {
            cell.height=SCREEN_WIDTH*image.size.height/image.size.width+10;
            markIV.width=SCREEN_WIDTH;
            markIV.height=SCREEN_WIDTH*image.size.height/image.size.width;
            [self.baseTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        }
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    CGFloat height = cell.height;
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return WFCGFloatY(300);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=[UIColor whiteColor];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0.00001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=HEXCOLOR(@"#F1F1F1");
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
    NSArray* arrr=[self.dataSource objectForKey:@"banner"];
    
    PYPhotoBrowseView *photoBroseView = [[PYPhotoBrowseView alloc] init];
    photoBroseView.imagesURL = arrr;
    photoBroseView.currentIndex = index;
    // photoBroseView.sourceImgageViews=@[pp];
    [photoBroseView show];
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
